function matched_ds = match_data( master_ds, loaded_var_struct, max_time_min)

% variables to skip
advm_vars = { ...
    'Amp1'      ,...
    'Amp2'      ,...
    'SNR1'      ,...
    'SNR2'       ...
    };

master_vars = get(master_ds,'VarNames');

% convert max_time_min from minutes to MATLAB date serial number
max_time_sn = datenum([0 0 0 0 max_time_min 0]);

% create a copy of the master dataset to add variables to
matched_ds = master_ds;

% get the names of the loaded variables
loaded_var_names = fieldnames(loaded_var_struct);

% for every loaded variable
for i = 1:length(loaded_var_names)
    
    % current variable name
    var_name = loaded_var_names{i};
    
    % look for advm variable names
    kAmp    = regexp(var_name,'Cell[0-9][0-9]Amp[0-9]');
    kSNR    = regexp(var_name,'Cell[0-9][0-9]SNR[0-9]');
    kadvm   = any(strcmp(var_name,advm_vars));
    
    % look for master dataset variable names
    kmvar   = any(strcmp(var_name,master_vars));
    
    % if var_name is not any variable that should be kept from the linear
    % model object, match it
    if ~any([kAmp;kSNR;kadvm;kmvar])
        
        % initialize variable observations to nan
        matched_ds.(var_name) = nan(length(matched_ds),...
            size(loaded_var_struct.(var_name).(var_name),2));
        
        % for every observation in the matched dataset
        for j = 1:length(matched_ds)
            
            % find the value and index of the minimum absolute time
            % difference
            [min_time_sn,min_time_index] = ...
                min(abs(matched_ds.DateTime(j) - ...
                loaded_var_struct.(var_name).DateTime));
            
            % if the minimum absolute time is less than than the maximum
            % allowable time then fill the observation value
            if min_time_sn < max_time_sn
                
                matched_ds.(var_name)(j,:) = ...
                    loaded_var_struct.(var_name).(var_name)(min_time_index,:);
                
            end
            
        end
                
    end
    
end

end

